package com.robcity.platform.controller;

import com.robcity.platform.api.RobotEventInfoService;
import com.robcity.platform.api.RobotLocationInfoService;
import com.robcity.platform.application.GlobalChannelGroup;
import com.robcity.platform.application.UdpClientServiceApplication;
import com.robcity.platform.common.Result;
import com.robcity.platform.enums.RespEnum;
import com.robcity.platform.param.HelloReq;
import com.robcity.platform.vo.RobotEventInfoVO;
import com.robcity.platform.vo.RobotLocationInfoVO;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/robot")
@Tag(name = "机器人")
public class RobotLocationInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RobotLocationInfoController.class);
    @Resource
    RobotLocationInfoService robotLocationInfoService;
    @Resource
    RobotEventInfoService robotEventInfoService;
    @Resource
    UdpClientServiceApplication udpClientServiceApplication;
    @Value("${llm.ip}")
    private String llmIp;
    @Value("${llm.port}")
    private Integer llmPort;
    @Value("${robot.dog.ip}")
    private String robotDogIp;
    @Value("${robot.dog.port}")
    private Integer robotDogPort;
    @Operation(summary = "查询机器人当前位置及状态")
    @RequestMapping(value = "/getRobotLocationInfo", method = RequestMethod.POST)
    public Result<List<RobotLocationInfoVO>> getRobotLocationInfo() {
        List<RobotLocationInfoVO> robotLocationInfoVOList = robotLocationInfoService.getRobotLocationInfo();
        return Result.success(robotLocationInfoVOList, RespEnum.SUCCESS.getMsg());
    }

    @Operation(summary = "查询事件")
    @RequestMapping(value = "/getRobotEventInfo", method = RequestMethod.POST)
    public Result<List<RobotEventInfoVO>> getRobotEventInfo() {
        List<RobotEventInfoVO> robotEventInfoVOList = robotEventInfoService.getRobotEventInfo();
        return Result.success(robotEventInfoVOList, RespEnum.SUCCESS.getMsg());
    }

    @Operation(summary = "测试发送web")
    @RequestMapping(value = "/helloWeb", method = RequestMethod.POST)
    public void helloWeb(@RequestBody HelloReq helloReq) {
        GlobalChannelGroup.channelGroup.forEach(o -> {
            //如果端口以8089结尾，说明这个channel是web
            if (o.localAddress().toString().endsWith("8090")) {
//                TextWebSocketFrame text = new TextWebSocketFrame(o.remoteAddress() + "发送消息：" + msg );
                o.writeAndFlush(helloReq.getMessage());
            }
        });
    }
    @Operation(summary = "测试发送给大漠型")
    @RequestMapping(value = "/helloLLM", method = RequestMethod.POST)
    public void helloLLM(@RequestBody HelloReq helloReq) {
        udpClientServiceApplication.sendUdpMessage(helloReq.getMessage(),llmIp, llmPort);
    }
    @Operation(summary = "测试发送给机器人")
    @RequestMapping(value = "/helloRobot", method = RequestMethod.POST)
    public void helloRobot(@RequestBody HelloReq helloReq) {
        udpClientServiceApplication.sendUdpMessage(helloReq.getMessage(),robotDogIp, robotDogPort);
    }
}
